package dashboard;

import java.io.IOException;
import java.util.*;

import javax.mail.Session;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class RegisterServlet extends HttpServlet  {

	private RegisterController RC = new RegisterController();

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		
		String[] allCourses = req.getParameterValues("courses");
		String firstName = req.getParameter("firstname");
		String name = req.getParameter("name");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String cpassword = req.getParameter("cpassword");
		String number = req.getParameter("number");
		String email = req.getParameter("email");
		String role = req.getParameter("role");
		HttpSession session = req.getSession();
		boolean finished = true;
		
		
		if(username != null || password != null  || email != null)
			if(RC.setInfo(username, password, cpassword, email, firstName, name, allCourses, number, role).size() != 0)
				finished = false;
		

		if(finished){
			RC.saveUser();
			session.setAttribute("rUser", null);
			Mail.getInstance().sendRegisterMsg(email, firstName + name);
			resp.sendRedirect("dashboard.jsp?R=true");
			
			
		}
		else
		{
			session.setAttribute("errors", RC.setInfo(username, password, cpassword, email, firstName, name, allCourses, number, role));
			session.setAttribute("rUser", RC.getUser());
			resp.sendRedirect("register.jsp?I=registered" );
		}
	}
}

